/** Car Selection Window **/

import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.swing.plaf.basic.*;
import javax.swing.border.*;
import javax.imageio.*;
import javax.*;
import java.io.*;
import java.awt.event.*;
import java.sql.*;

public class CarSelector{
  JFrame mainWindow;
  Container thePane;
  JPanel mainPanel, buttonPanel, selectPanel, buttonMenuPanel;
  JComboBox carSelector;
  JButton selectCar, newCar;
  JLabel selectCarLabel;
  User currUser;
  Car currCar;
  ArrayList<Car> cars = new ArrayList<Car>();
  AutoListener theListener;

  public Connection dbConnection = null;
  /*public String connectString = "jdbc:mysql://ec2-184-73-248-31.compute-1.amazonaws.com:3419/AutoBiography";
  public String driver = "com.mysql.jdbc.Driver";
  public String userid = "Team_AutoLytics";
  public String pwd = "COE1186";
  */
  public Statement stmnt = null;
  public ResultSet r = null;
  public CarSelector parent = null;

  public CarSelector(User curr, Connection dConnection){

      currUser = curr;
      parent = this;
      dbConnection = dConnection;
    // Establishing a connection with our online database.
    try{
    //  Class.forName(driver).newInstance();
    //  dbConnection = DriverManager.getConnection(connectString, userid, pwd);
    stmnt =  dbConnection.createStatement();
    //  System.out.println("Connection successful");
    }catch(Exception e){
     System.out.println("Connection Error.");
      System.out.println(e);
      System.exit(0);
    }
    
    //Load all cars
    try{
      r = stmnt.executeQuery("SELECT * FROM CARS WHERE USER = '" + currUser.username + "'");
        //choice = 7
      while(r.next()){
        cars.add(new Car(r.getString("MAKE"), r.getString("MODEL"), r.getString("VIN"), r.getString("USER"), r.getString("YEAR")));
      }
    }catch(SQLException e1){
      System.out.println("Error creating cars array.");
      System.out.println(e1);
    }
    
    mainWindow = new JFrame("AutoBiography");
    mainWindow.setSize(1200,800);
      
    mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    carSelector = new JComboBox();
      popResults();
    
    selectPanel = new JPanel();
    selectPanel.add(carSelector);
    selectPanel.setOpaque(false);

    theListener = new AutoListener();

    selectCar = new JButton("Select Car");
    selectCar.addActionListener(theListener);
    
    newCar = new JButton("New Car");
    newCar.addActionListener(theListener);
    
    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(1, 2));
    buttonPanel.add(newCar);
    buttonPanel.add(selectCar);
    buttonPanel.setOpaque(false);

    buttonMenuPanel = new JPanel();
    buttonMenuPanel.setLayout(new GridBagLayout());
    c.gridx = 0;
    c.gridy = 0;
    buttonMenuPanel.add(selectPanel, c);
    c.gridy = 1;
    buttonMenuPanel.add(buttonPanel, c);
    buttonMenuPanel.setOpaque(false);

    selectCarLabel = new JLabel("Please select the car you wish to view from the menu and click \"Select Car\", or click \"New Car\" to register a new vehicle.");
    selectCarLabel.setOpaque(false);

    mainPanel.setBackground(Color.white);
    c.anchor = GridBagConstraints.PAGE_START;
    mainPanel.add(selectCarLabel, c);
    c.anchor = GridBagConstraints.CENTER;
    c.ipady = 450;
    c.ipadx = 200;
    mainPanel.add(buttonMenuPanel, c);

    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thePane= mainWindow.getContentPane();
    
    thePane.add(mainPanel);
    mainWindow.setVisible(true);
  }

  private class AutoListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
      if(e.getSource() == newCar){
        mainWindow.setVisible(false);
        new NewCarMaker(mainWindow, currUser, dbConnection, parent);
          
      }
      else if(e.getSource() == selectCar){
        String makeModelYear;
        System.out.println(currUser.username);
        for(int i=0; i<cars.size(); i++){
          makeModelYear = cars.get(i).year+" "+cars.get(i).make+" "+cars.get(i).model;

          if(makeModelYear.equals(carSelector.getSelectedItem().toString()) && currUser.username.equals(cars.get(i).user)){
            currCar = cars.get(i);
          }
          //  System.out.println("Compare two:\nLeft  side:" + currUser.username + ".end");
          //  System.out.println("Right side:" +cars.get(i).user + ".end");
        }
          if (currCar != null){
            
            bluereader br = new bluereader();
            br.bluetoothreader("test", dbConnection, currCar);
            new AutoBiographyGUI(currUser, currCar, dbConnection); 
              mainWindow.setVisible(false);

          }
          else{
              System.out.println("No car selected to submit.");
          }
      }
    }
  }
    public void popResults(){
        try{
            carSelector.removeAllItems();
            r = stmnt.executeQuery("SELECT * FROM CARS WHERE USER = '"+ currUser.username + "'");
            //choice = 7
            while(r.next()){
                carSelector.addItem(r.getString("YEAR")+" "+r.getString("MAKE")+" "+r.getString("MODEL"));
            }
        }catch(SQLException e0){
            System.out.println("Error loading car names for user.");
            System.out.println(e0);
        }
    }
            
    public void addtoCars(Car incar){
        cars.add(incar);
    }
 //  public static void main(String[] args){
//     //new CarSelector();
//   }
}